// src/main/java/org/example/backend/service/StatisticsServiceImpl.java
package org.example.backend.service.Impl;

import org.example.backend.dto.StatisticsDto;
import org.example.backend.dto.SystemStatusDto;
import org.example.backend.repository.*;
import org.example.backend.service.StatisticsService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class StatisticsServiceImpl implements StatisticsService {

    private final StudentRepository studentRepository;
    private final TeacherRepository teacherRepository;
    private final CompanyRepository companyRepository;
    private final InternshipRepository internshipRepository;
    private final UserRepository userRepository;

    public StatisticsServiceImpl(StudentRepository studentRepository,
                               TeacherRepository teacherRepository,
                               CompanyRepository companyRepository,
                               InternshipRepository internshipRepository,
                               UserRepository userRepository) {
        this.studentRepository = studentRepository;
        this.teacherRepository = teacherRepository;
        this.companyRepository = companyRepository;
        this.internshipRepository = internshipRepository;
        this.userRepository = userRepository;
    }

    @Override
    public StatisticsDto getInternshipStatistics() {
        StatisticsDto stats = new StatisticsDto();

        // 基础统计数据
        stats.setTotalStudents(studentRepository.count());
        stats.setTotalTeachers(teacherRepository.count());
        stats.setTotalCompanies(companyRepository.count());
        stats.setTotalInternships(internshipRepository.count());

        // 实习状态统计
        long activeInternships = internshipRepository.countByStatus(1); // 进行中
        long completedInternships = internshipRepository.countByStatus(2); // 已完成
        stats.setActiveInternships(activeInternships);
        stats.setCompletedInternships(completedInternships);

        // 计算实习率 (有实习记录的学生/总学生数)
        long studentsWithInternship = internshipRepository.countDistinctStudents();
        long totalStudents = studentRepository.count();
        if (totalStudents > 0) {
            BigDecimal internshipRate = BigDecimal.valueOf((double) studentsWithInternship / totalStudents * 100)
                    .setScale(2, RoundingMode.HALF_UP);
            stats.setInternshipRate(internshipRate);
        }

        // 计算签约率 (已完成实习/总实习数)
        long totalInternships = internshipRepository.count();
        if (totalInternships > 0) {
            BigDecimal placementRate = BigDecimal.valueOf((double) completedInternships / totalInternships * 100)
                    .setScale(2, RoundingMode.HALF_UP);
            stats.setPlacementRate(placementRate);
        }

        // 实习状态分布
        Map<String, Object> statusDistribution = new HashMap<>();
        statusDistribution.put("未开始", internshipRepository.countByStatus(0));
        statusDistribution.put("进行中", internshipRepository.countByStatus(1));
        statusDistribution.put("已完成", internshipRepository.countByStatus(2));
        statusDistribution.put("已终止", internshipRepository.countByStatus(3));
        stats.setInternshipStatusDistribution(statusDistribution);

        return stats;
    }

    @Override
    public SystemStatusDto getSystemStatus() {
        SystemStatusDto status = new SystemStatusDto();

        // 系统基本信息
        status.setSystemVersion("1.0.0");
        status.setStartTime(new Date(System.currentTimeMillis() - 86400000)); // 假设系统运行了1天
        status.setTotalUsers(userRepository.count());

        // 模拟在线用户数
        status.setOnlineUsers(userRepository.countByStatus(1));

        // 模拟请求统计
        status.setTotalRequests(10000L);
        status.setErrorRequests(50L);

        // 模拟系统资源使用率
        status.setCpuUsage(45.5);
        status.setMemoryUsage(65.2);
        status.setDiskUsage(30.8);

        return status;
    }
}
